/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
// -*- coding:iso-8859-15 mode: c++ -*-
/** 
 * \file
 * \brief MaCI_SpeedCtrl Player for logged files.
 * \author Matthieu Myrsky <matthieu.myrsky@tkk.fi>
 *
 * $Id: SpeedCtrlFilePlayer.cpp,v 1.4 2009-06-24 13:10:49 morsko Exp $
 *
 */

#include "SpeedCtrlFilePlayer.hpp"

SpeedCtrlFilePlayer::SpeedCtrlFilePlayer( MaCI::SpeedCtrl::CSpeedCtrlServer *aSpeedCtrlServer,
                                        std::string aFileName)
  :PlayerModule(),
   iSpeedCtrlServer(aSpeedCtrlServer),
   iData(),
   iTimes(),
   iBegin(0,0),
   iEnd(0,0),
   iFile(NULL),
   iName("")
{
 
  
  dPrint(3,"Opening file '%s'",aFileName.c_str());
  iFile= fopen(aFileName.c_str(),"rt");
  char c[200];
  char interfaceName[64];
  //Read the four first lines..and get the interfacename...
  fgets (c, 200,iFile );

  fscanf(iFile,"%s %s %s %s",c,c,c,interfaceName);
  fgets (c, 200,iFile );
  fgets (c, 200,iFile );

  iName = "MaCI_SpeedCtrl."+std::string(interfaceName);

  ReadData();

  iSpeedCtrlServer->SetInterfaceInstanceName(std::string(interfaceName));
  iSpeedCtrlServer->Open();
}
//*****************************************************************************

SpeedCtrlFilePlayer::~SpeedCtrlFilePlayer()
{
  fclose(iFile);
  delete(iSpeedCtrlServer);
}
//*****************************************************************************

bool SpeedCtrlFilePlayer::ReadData()
{
  using namespace MaCI::SpeedCtrl;
  MaCI::Common::TTimestamp timestamp;
  float speed,angularSpeed, acceleration;
  int number = 1;
  bool first = true;
  while(number != EOF){
  
    number = fscanf(iFile,"%u %u %f %f %f",
                    &(timestamp.timestamp_s),
                    &(timestamp.timestamp_us),
                    &speed,
                    &angularSpeed,
                    &acceleration);
    if(first && number == 5){
      iBegin = timestamp.GetGimTime();
      dPrint(14,"timestamp %s, number %d",timestamp.GetGimTime().toString().c_str(),number);
      first = false;
    }
    //    dPrint(1,"timestamp %s, number %d",timestamp.GetGimTime().toString().c_str(),number);


    if(number >0){
      CSpeedCtrlData *speedData = new CSpeedCtrlData();
      speedData->CreateInternalBinBag();
      speedData->SetCommand(TCommand(KEventSpeed));
      speedData->SetTimestamp(timestamp);
      speedData->SetSpeed(TSpeed(speed,angularSpeed,acceleration));

      iData[timestamp.GetGimTime()] = speedData;
      iTimes.push_back(timestamp.GetGimTime());
    
    }
    

  }
  if(!first){
    iEnd = timestamp.GetGimTime();
  }
  dPrint(14,"timestamp %s, number %d",timestamp.GetGimTime().toString().c_str(),number);
  return true;

}
//*****************************************************************************

bool SpeedCtrlFilePlayer::OnTime(gim::time aTime)
{
  std::map<gim::time,MaCI::SpeedCtrl::CSpeedCtrlData *>::iterator iter
    =iData.find(aTime);

  if(iter != iData.end()){
    iSpeedCtrlServer->SendSpeedEvent(*(iter->second));
    return true;
  }else{

    return false;
  }

}
//*****************************************************************************

bool SpeedCtrlFilePlayer::GetTimes(std::list<gim::time> *aTimes)
{

  aTimes->assign(iTimes.begin(),iTimes.end());
  return true;
}
//*****************************************************************************

std::string SpeedCtrlFilePlayer::GetName(void)
{
  return iName;

}
//*****************************************************************************

void SpeedCtrlFilePlayer::GetTimeLimits(gim::time &aBegin,gim::time &aEnd)
{
  aBegin = iBegin;
  aEnd = iEnd;
  dPrint(14,"b %s, e %s", aBegin.toString().c_str(), aEnd.toString().c_str());
}
//*****************************************************************************




